//
//  EduViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "EduViewController.h"
#import "AddEduViewController.h"

#import "UILabel+HWDUILabel.h"
#import "EduCell.h"

@interface EduViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;
@property (nonatomic,strong)NSMutableArray *dataArr;//数据
@property (nonatomic,strong)UIButton *addBtn;//添加按钮
@end

@implementation EduViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"教育经历"];
    _dataArr = [NSMutableArray arrayWithArray:_data[@"jdUserLearningExperienceList"]];
    [self.view addSubview:self.tableV];
    [self.view addSubview:self.addBtn];
}
-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, SCREENH-64-60*screenW_6) style:UITableViewStyleGrouped];
        _tableV.estimatedRowHeight = 150*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.000001;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    EduCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EduCell"];
    if (!cell) {
        cell = [[EduCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"EduCell"];
    }
    [cell setDataDic:_dataArr[indexPath.row]];
    
    cell.chooseBtn.hidden = NO;
    cell.chooseBtn.tag = indexPath.row;
    UITapGestureRecognizer *chooseTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(chooseTap:)];
    [cell.chooseBtn addGestureRecognizer:chooseTap];
    return cell;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self deleteCell:indexPath];//删除
}

//编辑经历
-(void)chooseTap:(UITapGestureRecognizer *)tap
{
    AddEduViewController *vc = [AddEduViewController new];
    __weak typeof(self) weakSelf = self;
    vc.edu = [NSMutableDictionary dictionaryWithDictionary:_dataArr[tap.view.tag]];
    vc.block = ^(NSDictionary *dic){
        [weakSelf.dataArr replaceObjectAtIndex:tap.view.tag withObject:dic];
        [weakSelf.tableV reloadData];
        [weakSelf.data setObject:weakSelf.dataArr forKey:@"jdUserLearningExperienceList"];
        if (weakSelf.blcok) {
            weakSelf.blcok(weakSelf.data);
        }
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(UIButton *)addBtn//提交按钮
{
    if (!_addBtn) {
        _addBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(15*screenW_6,SCREENH-64-60*screenW_6, SCREENW-30*screenW_6, 45*screenW_6) targ:self sel:@selector(addSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"添加教育经历" image:@"" backGroundImage:@""];
        _addBtn.backgroundColor = [UIColor colorWithHexString:@"#59cbb3"];
        [_addBtn.layer setCornerRadius:5*screenW_6];
        _addBtn.layer.masksToBounds = YES;
    }
    return _addBtn;
}

-(void)addSelectBtn//提交事件
{
    AddEduViewController *vc = [AddEduViewController new];
    __weak typeof(self) weakSelf = self;
    vc.block = ^(NSDictionary *dic){
        [weakSelf.dataArr addObject:dic];
        [weakSelf.tableV reloadData];
        [weakSelf.data setObject:weakSelf.dataArr forKey:@"jdUserLearningExperienceList"];
        if (weakSelf.blcok) {
            weakSelf.blcok(weakSelf.data);
        }
    };
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)deleteCell:(NSIndexPath *)indexPath
{
    [HudHelper  showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/user/deleteLearningExperience" Dic:@{@"id":_dataArr[indexPath.row][@"id"]} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [_dataArr removeObjectAtIndex:indexPath.row];
                    [_data setObject:_dataArr forKey:@"jdUserLearningExperienceList"];
                    if (_blcok) {
                        _blcok(_data);
                    }
                    [_tableV deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

@end
